﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Solidbellstylereamer
    {
        public static void _Solidbellstylereamer(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 2; //放大系数

            double oal = 33.64;
            double lpr = 25.35;
            double sd1 = 1.58;
            double sd2 = 9.16;
            double bd = 18.52;
            double dcin1 = 2.47 * 2;
            double dcin2 = 1.56 * 2;
            double sta1 = 60;
            double sta2 = 88;
            double dconms = 8.53;

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 4, v1.Y);
            Vector2 v3 = new Vector2(v2.X - 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + bd / 2 - 0.5);
            Vector2 v5 = new Vector2(v4.X - 0.5, v4.Y + 0.5);
            Vector2 v6 = new Vector2(v3.X - lpr / 1.094, v5.Y);
            Vector2 v7 = new Vector2(v6.X - 0.5, v6.Y - 0.5);
            Vector2 v8 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v9 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v10 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v11 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v12 = new Vector2(v10.X, v3.Y);
            Vector2 v13 = new Vector2(v9.X, v3.Y);
            Vector2 v15 = new Vector2(v3.X - sd1, v3.Y + dcin1 / 2);
            Vector2 v14 = new Vector2(v3.X, v15.Y + sd1 * Math.Tan(sta1 / 2 * pi / 180));
            Vector2 v19 = new Vector2(v3.X - sd2, v3.Y + dcin2 / 2);
            Vector2 v18 = new Vector2(v19.X + sd2 / 5.872, v19.Y + sd2 / 5.872 * Math.Tan(sta2 / 2 * pi / 180));
            Vector2 v17 = new Vector2(v18.X + sd2 / 3.192, v18.Y);
            Vector2 v16 = new Vector2(v17.X + sd2 / 13.086, v15.Y);
            Vector2 v20 = new Vector2(v19.X - lpr / 16.9, v19.Y);
            Vector2 v21 = new Vector2(v20.X - lpr / 50.7, v20.Y + lpr / 50.7);
            Vector2 v22 = new Vector2(v21.X - lpr / 13.703, v21.Y);
            Vector2 v23 = new Vector2(v22.X - lpr / 50.7, v22.Y + lpr / 50.7);
            Vector2 v24 = new Vector2(v23.X - lpr / 11.368, v23.Y);
            Vector2 v25 = new Vector2(v24.X - lpr / 25.606, v24.Y - lpr / 25.606);
            Vector2 v26 = new Vector2(v25.X, v3.Y);
            Vector2 v27 = new Vector2(v24.X, v3.Y);
            Vector2 v28 = new Vector2(v23.X, v3.Y);
            Vector2 v29 = new Vector2(v22.X, v3.Y);
            Vector2 v30 = new Vector2(v21.X, v3.Y);
            Vector2 v31 = new Vector2(v20.X, v3.Y);
            Vector2 v32 = new Vector2(v19.X, v3.Y);
            Vector2 v33 = new Vector2(v18.X, v3.Y);
            Vector2 v34 = new Vector2(v17.X, v3.Y);
            Vector2 v35 = new Vector2(v16.X, v3.Y);
            Vector2 v36 = new Vector2(v25.X - lpr / 31.6875, v25.Y + bd / 97.474);
            Vector2 v37 = new Vector2(v36.X - lpr / 6.584, v36.Y + bd / 4.773);
            Vector2 v38 = new Vector2(v36.X - lpr / 5.494, v6.Y);
            Vector2 v39 = new Vector2(v36.X, v3.Y + dcin1 /1.235);
            Vector2 v40 = new Vector2(v4.X, v39.Y);
            Vector2 v41 = new Vector2(v7.X, v3.Y + bd / 2.43);
            Vector2 v42 = new Vector2(v3.X - lpr + 0.3, v41.Y);
            Vector2 v43 = new Vector2(v42.X - 0.3, v42.Y - 0.3);
            Vector2 v44 = new Vector2(v43.X, v43.Y * -1);
            Vector2 v45 = new Vector2(v42.X, v42.Y * -1);
            Vector2 v46 = new Vector2(v41.X, v41.Y * -1);
            Vector2 v47 = new Vector2(v43.X, v3.Y + dconms / 2.091);
            Vector2 v48 = new Vector2(v47.X - 0.3, v47.Y);
            Vector2 v49 = new Vector2(v48.X - lpr / 2 + lpr / 2.091, v3.Y + dconms / 2);
            Vector2 v50 = new Vector2(v3.X - oal + 0.8, v49.Y);
            Vector2 v51 = new Vector2(v50.X - 0.8, v50.Y - 0.5);
            Vector2 v52 = new Vector2(v51.X, v51.Y * -1);
            Vector2 v53 = new Vector2(v50.X, v50.Y * -1);
            Vector2 v54 = new Vector2(v49.X, v49.Y * -1);
            Vector2 v55 = new Vector2(v48.X, v48.Y * -1);
            Vector2 v56 = new Vector2(v47.X, v47.Y * -1);
            Vector2 v57 = new Vector2(v43.X, v3.Y + dconms / 2.787);
            Vector2 v58 = new Vector2(v41.X, v57.Y);
            Vector2 v59 = new Vector2(v58.X, v58.Y * -1);
            Vector2 v60 = new Vector2(v57.X, v57.Y * -1);
            Vector2 v61 = new Vector2(v42.X, v58.Y);
            Vector2 v62 = new Vector2(v45.X, v59.Y);
            Vector2 v63 = new Vector2(v15.X, v3.Y);
            Vector2 v64 = fun.Polar(v18, sta2 / 2 * pi / 180, 3);
            Vector2 v65 = fun.Polar(v14, sta1 / 2 * pi / 180, 3);
            Vector2 a1 = new Vector2(v18.X - (v18.X - v19.X) / 2, v18.Y - (v18.Y - v19.Y) / 2);
            Vector2 a2 = new Vector2(v19.X + oal / 1.97, v19.Y);
            Vector2 a3 = new Vector2(v15.X + oal / 2.049, v15.Y);
            Vector2 a4 = new Vector2(v14.X + oal / 1.586, v14.Y);
            


            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v13));
            fun.PolyFill(dxf, line2, 0.2, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v14));
            line3.Vertexes.Add(new LwPolylineVertex(v15));
            line3.Vertexes.Add(new LwPolylineVertex(v63));
            line3.Vertexes.Add(new LwPolylineVertex(v15));
            line3.Vertexes.Add(new LwPolylineVertex(v16));
            line3.Vertexes.Add(new LwPolylineVertex(v35));
            line3.Vertexes.Add(new LwPolylineVertex(v16));
            line3.Vertexes.Add(new LwPolylineVertex(v17));
            line3.Vertexes.Add(new LwPolylineVertex(v34));
            line3.Vertexes.Add(new LwPolylineVertex(v17));
            line3.Vertexes.Add(new LwPolylineVertex(v18));
            line3.Vertexes.Add(new LwPolylineVertex(v33));
            line3.Vertexes.Add(new LwPolylineVertex(v18));
            line3.Vertexes.Add(new LwPolylineVertex(v19));
            line3.Vertexes.Add(new LwPolylineVertex(v32));
            line3.Vertexes.Add(new LwPolylineVertex(v19));
            line3.Vertexes.Add(new LwPolylineVertex(v20));
            line3.Vertexes.Add(new LwPolylineVertex(v31));
            line3.Vertexes.Add(new LwPolylineVertex(v20));
            line3.Vertexes.Add(new LwPolylineVertex(v21));
            line3.Vertexes.Add(new LwPolylineVertex(v30));
            line3.Vertexes.Add(new LwPolylineVertex(v21));
            line3.Vertexes.Add(new LwPolylineVertex(v22));
            line3.Vertexes.Add(new LwPolylineVertex(v29));
            line3.Vertexes.Add(new LwPolylineVertex(v22));
            line3.Vertexes.Add(new LwPolylineVertex(v23));
            line3.Vertexes.Add(new LwPolylineVertex(v28));
            line3.Vertexes.Add(new LwPolylineVertex(v23));
            line3.Vertexes.Add(new LwPolylineVertex(v24));
            line3.Vertexes.Add(new LwPolylineVertex(v27));
            line3.Vertexes.Add(new LwPolylineVertex(v24));
            line3.Vertexes.Add(new LwPolylineVertex(v25));
            line3.Vertexes.Add(new LwPolylineVertex(v26));
            fun.PolyFill(dxf, line3, 0.2, contour);

            Arc c1 = fun.DrawArc3(v37, v36, v25);
            fun.drawBArc(dxf, c1, 0.2, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v38));
            line4.Vertexes.Add(new LwPolylineVertex(v37));
            line4.Vertexes[0].Bulge = Math.Tan(23 * pi / 180 / 4);
            fun.PolyFill(dxf, line4, 0.2, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v36));
            line5.Vertexes.Add(new LwPolylineVertex(v39));
            line5.Vertexes.Add(new LwPolylineVertex(v40));
            fun.PolyFill(dxf, line5, 0.2, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v61));
            line6.Vertexes.Add(new LwPolylineVertex(v42));
            line6.Vertexes.Add(new LwPolylineVertex(v41));
            line6.Vertexes.Add(new LwPolylineVertex(v42));
            line6.Vertexes.Add(new LwPolylineVertex(v43));
            line6.Vertexes.Add(new LwPolylineVertex(v44));
            line6.Vertexes.Add(new LwPolylineVertex(v45));
            line6.Vertexes.Add(new LwPolylineVertex(v62));
            line6.Vertexes.Add(new LwPolylineVertex(v45));
            line6.Vertexes.Add(new LwPolylineVertex(v46));
            fun.PolyFill(dxf, line6, 0.2, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v58));
            line7.Vertexes.Add(new LwPolylineVertex(v57));
            line7.Vertexes.Add(new LwPolylineVertex(v59));
            line7.Vertexes.Add(new LwPolylineVertex(v60));
            line7.Vertexes.Add(new LwPolylineVertex(v58));
            fun.PolyFill(dxf, line7, 0.2, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v48));
            line8.Vertexes.Add(new LwPolylineVertex(v49));
            line8.Vertexes.Add(new LwPolylineVertex(v54));
            line8.Vertexes.Add(new LwPolylineVertex(v55));
            line8.Vertexes.Add(new LwPolylineVertex(v48));
            line8.Vertexes.Add(new LwPolylineVertex(v55));
            line8.Vertexes.Add(new LwPolylineVertex(v56));
            line8.Vertexes.Add(new LwPolylineVertex(v47));
            line8.Vertexes.Add(new LwPolylineVertex(v48));
            fun.PolyFill(dxf, line8, 0.2, contour);
                                                            
            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v50));
            line9.Vertexes.Add(new LwPolylineVertex(v53));
            line9.Vertexes.Add(new LwPolylineVertex(v52));
            line9.Vertexes.Add(new LwPolylineVertex(v51));
            line9.Vertexes.Add(new LwPolylineVertex(v50));
            line9.Vertexes.Add(new LwPolylineVertex(v49));
            line9.Vertexes.Add(new LwPolylineVertex(v54));
            line9.Vertexes.Add(new LwPolylineVertex(v53));
            fun.PolyFill(dxf, line9, 0.2, contour);

            LwPolyline line10 = new LwPolyline();
            line10.Vertexes.Add(new LwPolylineVertex(v64));
            line10.Vertexes.Add(new LwPolylineVertex(v18));
            fun.PolyFill(dxf, line10, 0.05, dimensioning);

            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v65));
            line11.Vertexes.Add(new LwPolylineVertex(v14));
            fun.PolyFill(dxf, line11, 0.05, dimensioning);

            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v19));
            line12.Vertexes.Add(new LwPolylineVertex(a2));
            fun.PolyFill(dxf, line12, 0.05, dimensioning);

            LwPolyline line13 = new LwPolyline();
            line13.Vertexes.Add(new LwPolylineVertex(v15));
            line13.Vertexes.Add(new LwPolylineVertex(a3));
            fun.PolyFill(dxf, line13, 0.05, dimensioning);

            LwPolyline line14 = new LwPolyline();
            line14.Vertexes.Add(new LwPolylineVertex(v14));
            line14.Vertexes.Add(new LwPolylineVertex(a4));
            fun.PolyFill(dxf, line14, 0.05, dimensioning);

            fun.drawcircle(dxf, v15, 1, 0.05, dimensioning);

            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v38));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v37));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v36));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v25));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v26));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(new Vector2(v41.X, v13.Y)));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v7));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v6));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v38));
            hatchpoly1.Vertexes[0].Bulge = Math.Tan(23 * pi / 180 / 4);
            hatchpoly1.Vertexes[1].Bulge = Math.Tan(56 * pi / 180 / 4);
            hatchpoly1.Vertexes[2].Bulge = Math.Tan(8 * pi / 180 / 4);
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);



            //坐标系
            fun.Axis(dxf, new Vector2(v43.X, v3.Y), -90, 180, "", "", "PCS,MCS", 0.5, 2, 0);

            fun.Axis(dxf, v3, -90, 180, "", "", "CIP", 0.5, 2, 0);


            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;


            fun.dimrot1(dxf, v15, v4, 0, bd / 2, 1, height, arrowsize, exline, "SD_1", "", "");

            fun.dimrot1(dxf, v15, a1, 0, bd / 2.4, 1, height, arrowsize, exline, "SDL_1", "", "");

            fun.dimrot1(dxf, v19, v4, 0, bd / 1.5, 1, height, arrowsize, exline, "SD_2", "", "");

            fun.dimrot1(dxf, v24, v4, 0, bd, 1, height, arrowsize, exline, "LU", "", "");

            fun.dimrot1(dxf, v43, v4, 0, bd * 1.2, 1, height, arrowsize, exline, "LPR", "", "");

            fun.dimrot1(dxf, v51, v4, 0, bd * 1.5, 1, height, arrowsize, exline, "OAL", "", "");

            fun.dimrot1(dxf, v63, v11, 180, bd / 1.2, 1, height, arrowsize, exline, "PLGL", "", "");

            fun.dimrot1(dxf, v63, v44, 180, bd / 1.2 + (v63.Y + v44.Y) / 2 - (v63.Y + v11.Y) / 2, 1, height, arrowsize, exline, "LF,LTA", "", "");

            fun.dimrot1(dxf, v52, v44, 180, bd / 1.2 + (v52.Y + v44.Y) / 2 - (v63.Y + v11.Y) / 2, 1, height, arrowsize, exline, "LS", "", "");

            fun.dimrot1(dxf, v50, v53, 90, dconms / 2, 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.dimrot1(dxf, v5, v10, -90, oal / 1.12, 1, height, arrowsize, exline, "BD", "", "");

            fun.jt(dxf, new Vector2(a2.X - 0.3, a2.Y), -90, 6, height, "DCIN_2");

            fun.jt(dxf, new Vector2(a3.X - 0.3, a3.Y), -90, 5.5, height, "DCIN_1");

            fun.jt(dxf, new Vector2(a4.X - 0.3, a4.Y), -90, 5, height, "DCINTF");

            fun.jt(dxf, new Vector2(v49.X -1,v49.Y), 110, 3, height, "CCMS");

            fun.jt(dxf, fun.Polar(v14, sta1 / 2 * pi / 180, sd2 / 2), -90, 5, height, "STA_1");

            fun.jt(dxf, fun.Polar(v18, sta2 / 2 * pi / 180, 2.8), -90, 10, height, "STA_2");

            fun.jt(dxf, new Vector2(v2.X + oal / 2.827, v2.Y - bd / 1.556), 180, 3, height, "FDP");

            fun.jdbz2(dxf, new Line(v14, fun.Polar(v14, 0 * pi / 180, 0.1)), new Line(v14, fun.Polar(v14, sta1 / 2 * pi / 180, 0.1)), sd2, height, arrowsize, exline, "PLGANG", "", "");

            fun.text1(dxf, new Vector2(v14.X - 3, v14.Y + 2.5), 90, "CRP");




            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}